using System;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.HTML.Components;

namespace ASPNETVN.PORTAL.Modules.HTML.Controls
{
    public partial class AddEditItem : AddEditItemPage
    {
        protected override void OnInit(EventArgs e)
        {
            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            ButtonDelete = this.btnDelete;

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Item item = Item.Load(this.ItemID);
                if (item != null)
                {
                    txtHtmlContent.Text = Server.HtmlDecode(item.HtmlContent);
                }
            }
        }

        protected override void OnUpdate(EventArgs e)
        {
            base.OnUpdate(e);

            if (IsValid)
            {
                if (this.ItemID == 0)
                {
                    Item.Insert(Server.HtmlEncode(txtHtmlContent.Text), this.ModuleID);
                }
                else
                {
                    Item.Update(this.ItemID, Server.HtmlEncode(txtHtmlContent.Text), this.ModuleID);
                }
                this.ReturnToReferrerPage();
            }
        }

        protected override void OnDelete(EventArgs e)
        {
            base.OnDelete(e);
            if (this.ItemID > 0)
            {
                Item.Delete(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        protected override void OnCancel(EventArgs e)
        {
            base.OnCancel(e);
            this.ReturnToReferrerPage();
        }
    }
}